﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Message_ShowController : UserController
    {
        public IUser_messageService _user_message { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(int id)
        {
            //检查用户是否登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时!" });
            }
            if (!_user_message.Exists(id))
            {
                return Ok(new { status = 0, msg = "内容不存在!" });
            }
            var msg = _user_message.GetModel(id);
            if (msg.accept_user_name != userModel.user_name && msg.post_user_name != userModel.user_name)
            {
                return Ok(new { status = 0, msg = "您所查看的并非自己的短消息!" });
            }
            //设为已阅读状态
            _user_message.UpdateField(id, "is_read=1,read_time='" + DateTime.Now + "'");
            return Ok(new { status = 1, data = msg });
        }
    }
}